package com.fuqi.android.zhongcai.base.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.Constants;
import com.fuqi.android.zhongcai.base.model.ImageItem;
import com.fuqi.android.zhongcai.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class AddImgAdapter extends BaseAdapter {
    private List<ImageItem> pictures = new ArrayList<ImageItem>();
    private Context mContext;

    public AddImgAdapter(Context context, List<ImageItem> dataList) {
        this.mContext = context;
        this.pictures = dataList;
    }

    public int getCount() {

        if (pictures == null || pictures.size() == 0) {
            return 1;
        } else if (pictures.size() == Constants.MAX_IMAGE_SIZE) {
            return Constants.MAX_IMAGE_SIZE;
        } else {
            return pictures.size() + 1;
        }
    }

    public Object getItem(int position) {
        if (pictures != null
                && pictures.size() == Constants.MAX_IMAGE_SIZE) {
            return pictures.get(position);
        }

        else if (pictures == null || position - 1 < 0
                || position > pictures.size()) {
            return null;
        } else {
            return pictures.get(position - 1);
        }
    }

    public long getItemId(int position) {
        return position;
    }

    ViewHolder viewHolder = null;

    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.item_publish, null);
            viewHolder.imgShow = (ImageView) convertView
                    .findViewById(R.id.item_grid_image_show);
            viewHolder.imgAdd = (ImageView) convertView
                    .findViewById(R.id.item_grid_image_add);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        if (isShowAddItem(position)) {
            viewHolder.imgAdd.setVisibility(View.VISIBLE);
            viewHolder.imgShow.setVisibility(View.GONE);
            viewHolder.imgAdd.setBackgroundResource(R.color.trans);
        } else {
            viewHolder.imgAdd.setVisibility(View.GONE);
            Glide.with(mContext)
                    .load(pictures.get(position)
                            .sourcePath)
                    .into(viewHolder.imgShow);

//            Glide.with(getActivity())
//                    .load(data.getHeadimg())
////                            .placeholder(R.drawable.default_empty_img)
//                    .centerCrop()
//                    .crossFade()
//                    .into((CircleImageView) holder.getView(R.id.recyclerview_item_invest_avatarimag));
            LogUtils.i("item.thumbnailPath: " + pictures.get(position).thumbnailPath + "  item.sourcePath: " + pictures.get(position).sourcePath);
            }
//            viewHolder.imgDel.setOnClickListener(new OnClickListener() {
//
//                @Override
//                public void onClick(View v) {
//                // TODO Auto-generated method stub
//                if (pictures.size() == 1) {
//                    removeImgs();
//                } else {
//                    removeImg(position);
//
//                }
//                notifyDataSetChanged();
//            }
//        });
        return convertView;
    }

    private void removeImgs() {
        pictures.clear();
    }

    private void removeImg(int location) {
        if (location + 1 <= pictures.size()) {
            pictures.remove(location);
        }
    }

    private boolean isShowAddItem(int position) {
        int size = pictures == null ? 0 : pictures.size();
        return position == size;
    }

    public class ViewHolder {

        ImageView imgShow;
        ImageView imgAdd;
    }


    public List<ImageItem> getPictures(){


        return pictures;
    }

    public void setPictures(List<ImageItem> pictures) {
        this.pictures = pictures;
    }


}
